package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.customView.ConfirmDialog;
import com.dlc.felear.xiaoensale.dao.MenuFoodMaterialDao;
import com.dlc.felear.xiaoensale.entity.MenuFoodMaterial;

import java.util.ArrayList;
import java.util.List;


public class FoodMaterialControlAdapter extends BaseAdapter implements ConfirmDialog.ConfirmDialogCallback{

    private static final String TAG = "FoodMaterialControlAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<MenuFoodMaterial> items;
    private MenuFoodMaterialDao foodMaterialDao;

    private int tempIndex=0;
    private ConfirmDialog confirmDialog;


    public FoodMaterialControlAdapter(Context context) {
        this.context = context;
        items = new ArrayList<>();
        foodMaterialDao=new MenuFoodMaterialDao(context);
    }

    public List<MenuFoodMaterial> getData() {
        return items;
    }


    public void setItems(List<MenuFoodMaterial> data) {
        items.clear();
        if (data==null||data.isEmpty()) {
            notifyDataSetChanged();
            return;
        }
        items.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public MenuFoodMaterial getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_food_material_2, null);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_delete = convertView.findViewById(R.id.tv_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tempIndex=position;
                confirmDelete();
            }
        });
        holder.tv_name.setText(items.get(position).getFoodName());
        return convertView;
    }

    private void confirmDelete(){
        MenuFoodMaterial foodMaterial=items.get(tempIndex);
        showDialog(foodMaterial.getFoodName());
    }

    private void showMsg(String msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void selectResult(int value, int id) {
        if(value==1){
            MenuFoodMaterial foodMaterial=items.get(tempIndex);
            foodMaterialDao.delete(foodMaterial);
            items.remove(foodMaterial);
            notifyDataSetChanged();
            showMsg("删除成功");
        }
    }


    static class ViewHolder {
        TextView tv_name,tv_delete;
    }

    private void showDialog(String name){
        if(confirmDialog==null){
            confirmDialog=new ConfirmDialog(context,this);
        }
        confirmDialog.showDialog("确定删除:"+name);
    }

}